<?php
/**
 * Library Of Shared Code (LOSC)
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   LOSC Framework
 * @package    View
 * @subpackage Helper
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */

/**
 * Helper for a simple toolbar
 *
 * @category   LOSC Framework
 * @package    View
 * @subpackage Helper
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */
class Losc_View_Helper_Toolbar extends Zend_View_Helper_Abstract
{
    /**
     * What string to use as the indentation of output, this will typically be
     * spaces. e.g. '    '
     * 
     * @var string
     */
    private $_indent = '';
    
    /**
     * Toolbar items to put on right-hand side
     *
     * @var array
     */
    private $_left = array();
    
    /**
     * Toolbar items to put on right-hand side
     *
     * @var array
     */
    private $_right = array();

    /**
     * Retrieve placeholder for title element and optionally set state
     *
     * @param  string $item
     * @param  string $side
     * @return Losc_View_Helper_Toolbar
     */
    public function toolbar($item = null, $side = null)
    {
        if ($item) {
            if (!$side == 'right') {
                $side = 'left';
            }
            
            $p = '_' . $side;
            
            if (is_array($item)) {
                foreach ($item as $toolbarItem) {
                    $this->{$p}[] = $toolbarItem;
                }
            } else {
                $this->{$p}[] = $item;
            }
        }

        return $this;
    }

    /**
     * Set the indentation string for __toString() serialization,
     * optionally, if a number is passed, it will be the number of spaces
     *
     * @param  string|int $indent
     * @return Zend_View_Helper_Placeholder_Container_Abstract
     */
    public function setIndent($indent)
    {
        $this->_indent = $this->getWhitespace($indent);
        return $this;
    }

    /**
     * Retrieve indentation
     *
     * @return string
     */
    public function getIndent()
    {
        return $this->_indent;
    }

    /**
     * Retrieve whitespace representation of $indent
     * 
     * @param  int|string $indent 
     * @return string
     */
    public function getWhitespace($indent)
    {
        if (is_int($indent)) {
            $indent = str_repeat(' ', $indent);
        }

        return (string) $indent;
    }

    /**
     * Turn helper into string
     *
     * @param  string|null $indent
     * @param  string|null $locale
     * @return string
     */
    public function toString($indent = null)
    {
        $indent = (null !== $indent)
                ? $this->getWhitespace($indent)
                : $this->getIndent();

        $lc = count($this->_left);
        $rc = count($this->_right);
        
        if (($lc + $rc) < 1) {
            return '';
        }

        $output = "$indent<div class=\"toolbar\">\n";
    
        if ($lc > 0) {
            $output .= "$indent    <ul class=\"left\">\n";
            foreach ($this->_left as $item) {
                $output .= "$indent        <li>$item</li>\n";
            }
            $output .= "$indent    </ul>\n";
        }
    
        if ($rc > 0) {
            $output .= "$indent    <ul class=\"right\">\n";
            foreach ($this->_right as $item) {
                $output .= "$indent        <li>$item</li>\n";
            }
            $output .= "$indent    </ul>\n";
        }
        
        $output .= "$indent    <div class=\"clear\"></div>\n";
        
        $output .= "$indent</div>\n";

        return $output;
    }
}
